package com.example.schedulecard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schedulecard.DataTransfer.ScheduleNameTransfer;
import com.example.schedulecard.Databases.CourseDatabaseOption;
import com.example.schedulecard.Entity.Course;

public class CourseUpdata extends AppCompatActivity {
//    定义课程名 教师名 教室名 三个输入框
    EditText updataCourseName;
    EditText updataTeacher;
    EditText updataClassRoom;

    TextView add_name;
//    定义星期 课程开始 课程结束 三个下拉框
    Spinner updataDay;
    Spinner updataStart;
    Spinner updataEnd;
//    新建按钮
    Button updataButton, reButton;

    ImageView back;
//    声明数据库
    SQLiteDatabase sqLiteDatabase;

    Course course;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_courses);
        Init();
    }

    private void Init() {
//        定位编辑框
        updataCourseName = (EditText) findViewById(R.id.course_name);
        updataTeacher = (EditText) findViewById(R.id.teacher_name);
        updataClassRoom = (EditText) findViewById(R.id.class_room);
//        定位标题并写入信息
        add_name = (TextView) findViewById(R.id.add_name);
        add_name.setText("修改课程");
//        定位下拉框
        updataDay = (Spinner) findViewById(R.id.week);
        //        设置星期下拉框的内容
        ScheduleNameTransfer.setweekdayspinner(this,ScheduleNameTransfer.scheduleinfo,updataDay);

        updataStart = (Spinner) findViewById(R.id.classes_begin);
        updataEnd = (Spinner) findViewById(R.id.classes_ends);
//        接收数据
        Intent intent = getIntent();
        course = (Course) intent.getSerializableExtra("courseinfo");
//        初始化修改的课程信息
        setCourseInfo(course);
//        打开数据库
        sqLiteDatabase = CourseDatabaseOption.CreateCoursetable(CourseUpdata.this)
                .getWritableDatabase();
//        定位添加和返回按钮
        updataButton = (Button) findViewById(R.id.add_courses_button);
        reButton = (Button) findViewById(R.id.add_re_button);
        back = (ImageView) findViewById(R.id.add_courses_back);

//        为添加按钮添加单击事件
        updataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Course xcourse = new Course(updataCourseName.getText().toString(),
                        updataTeacher.getText().toString(),
                        updataClassRoom.getText().toString(),
                        Integer.valueOf(updataDay.getSelectedItem().toString()),
                        Integer.valueOf(updataStart.getSelectedItem().toString()),
                        Integer.valueOf(updataEnd.getSelectedItem().toString()),
                        ScheduleNameTransfer.scheduleinfo.getSchedulename()
                );
                if (xcourse.getClassRoom().equals("") || xcourse.getTeacher().equals("")
                        || xcourse.getCourseName().equals("")) {
                    Toast.makeText(CourseUpdata.this, "基本课程信息未填写",
                            Toast.LENGTH_SHORT).show();
                } else {
                    if (xcourse.getClassStart() > xcourse.getClassEnd()) {
                        Toast.makeText(CourseUpdata.this, "课程开始时间晚于结束时间，请重新选择!",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        if (CourseDatabaseOption.updatecourse(sqLiteDatabase, course, xcourse) > 0) {
                            Toast.makeText(CourseUpdata.this, "课程信息修改成功!",
                                    Toast.LENGTH_SHORT).show();
                            Intent intenthome = new Intent(CourseUpdata.this, ScheduleCardHome.class);
                            intenthome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intenthome);
                            finish();
                        } else {
                            Toast.makeText(CourseUpdata.this, "课程信息修改失败!\n" +
                                    "请检查课程开始或结束是否与其他课程重复", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            }
        });
//        为重置按钮添加单击事件
        reButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updataCourseName.setText("");
                updataTeacher.setText("");
                updataClassRoom.setText("");
                updataDay.setSelection(0);
                updataStart.setSelection(0);
                updataEnd.setSelection(0);
            }
        });
//        为返回按钮添加单击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

//    初始化课程信息数据
    private void setCourseInfo(Course course) {
//        初始化编辑框
        updataCourseName.setText(course.getCourseName());
        updataTeacher.setText(course.getTeacher());
        updataClassRoom.setText(course.getClassRoom());
//        初始化下拉框,因为下拉框编号从0开始，因此每个数值减1
        updataDay.setSelection(course.getDay() - 1);
        updataStart.setSelection(course.getClassStart() - 1);
        updataEnd.setSelection(course.getClassEnd() - 1);
    }
}